import  OpenAI  from 'openai';
 
const client = new OpenAI({
    apiKey: "sk-1sYUNLDfMIoKDiOltK2FQucTCY7SL9CvbHzWv4nbELtDs4U6",    
    baseURL: "https://api.moonshot.cn/v1",
    dangerouslyAllowBrowser: true,
});
 
export async function kimiGpt(params) {
    let { message } = params;

    if (!message) {
        throw new Error("Message is required.");
    }

    message = `根据表结构生成typeorm的实体。
                要求1: 当列名有下划线时，成员变量命名为驼峰形式，属性中的name命名为下划线格式例如
                @Column({ name:  'user_id' })
                userId: number;
                要求2: 实体类命名为表名的驼峰形式加“Entity”后缀
                要求3: create_time使用注解"@CreateDateColumn()"
                要求4: id使用注解"@PrimaryGeneratedColumn()"
                表结构如下
                ${message}`;
    const completion = await client.chat.completions.create({
        model: "moonshot-v1-8k",         
        messages: [{ role: "user", content: message }],
        temperature: 0.3
    });
    return completion.choices?.[0]?.message?.content;
}

export async function generateModule(params) {
    window['api'].generateModule(params);
}